package com.chujiekeji.test.activity;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatTextView;
import android.view.KeyEvent;
import android.view.View;

import com.chujiekeji.test.BaseApplication;
import com.chujiekeji.test.fragment.FriendCircleFragment;
import com.chujiekeji.test.fragment.PublishFragment;
import com.chujiekeji.test.R;
import com.chujiekeji.test.adapter.BaseFragmentAdapter;
import com.chujiekeji.test.base.BaseActivity;
import com.chujiekeji.test.fragment.HomeFragment;
import com.chujiekeji.test.fragment.FriendFragment;
import com.chujiekeji.test.fragment.UserInfoFragment;

import java.util.ArrayList;
import java.util.List;

public class IndexActivity extends BaseActivity {
    public static final String ACTION_NOTICE = "ACTION_NOTICE";
    private ViewPager mainViewPager;
    private AppCompatTextView[] navigationTextView;
    private List<Fragment> fragmentList;
    private long exitTimeLong;
    private ProgressDialog progressDialog;
    private BroadcastReceiver broadcastReceiver;

    private Drawable[] navigationPressDrawable;
    private Drawable[] navigationNormalDrawable;


    @Override
    public void initView() {
        setContentView(R.layout.activity_index);
        mainViewPager = findViewById(R.id.mainViewPager);
        navigationTextView = new AppCompatTextView[5];
        navigationTextView[0] = findViewById(R.id.homeTextView);
        navigationTextView[1] = findViewById(R.id.friendTextView);
        navigationTextView[2] = findViewById(R.id.pubTextView);
        navigationTextView[3] = findViewById(R.id.cartTextView);
        navigationTextView[4] = findViewById(R.id.mineTextView);

        navigationNormalDrawable = new Drawable[navigationTextView.length];
        navigationNormalDrawable[0] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_home);
        navigationNormalDrawable[1] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_cate);
        navigationNormalDrawable[2] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_pub);
        navigationNormalDrawable[3] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_help_circle);
        navigationNormalDrawable[4] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_mine);

        navigationPressDrawable = new Drawable[navigationTextView.length];
        navigationPressDrawable[0] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_home_press);
        navigationPressDrawable[1] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_cate_press);
        navigationPressDrawable[2] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_pub_press);
        navigationPressDrawable[3] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_help_circle_press);
        navigationPressDrawable[4] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_mine_press);
    }

    @Override
    public void initData() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new FriendFragment());
        fragmentList.add(new PublishFragment());
        fragmentList.add(new FriendCircleFragment());
        fragmentList.add(new UserInfoFragment());
        mainViewPager.setAdapter(new BaseFragmentAdapter(getSupportFragmentManager(), fragmentList));
        mainViewPager.setOffscreenPageLimit(navigationTextView.length);

        broadcastReceiver = null;
        updateNavigation(0);
    }

    @Override
    public void initEven() {
        mainViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                updateNavigation(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        for (int i = 0; i < navigationTextView.length; i++) {
            final int position = i;
            navigationTextView[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateNavigation(position);
//                    if (v.getId()==R.id.pubTextView){
//                        BaseApplication.context().start(getActivity(),PubActivity.class);
//                    }
                }
            });
        }
    }

    private void updateNavigation(int position) {

        for (int i = 0; i < navigationTextView.length; i++) {
            navigationTextView[i].setTextColor(BaseApplication.get().getColors(R.color.grey));
            navigationTextView[i].setCompoundDrawablesWithIntrinsicBounds(null, navigationNormalDrawable[i], null, null);
        }

        navigationTextView[position].setTextColor(BaseApplication.get().getColors(R.color.primary));
        navigationTextView[position].setCompoundDrawablesWithIntrinsicBounds(null, navigationPressDrawable[position], null, null);
        mainViewPager.setCurrentItem(position);

    }
}
